/*
 * @lc app=leetcode.cn id=1614 lang=typescript
 *
 * [1614] 括号的最大嵌套深度
 */

// @lc code=start
function maxDepth(s: string): number {
    let stack: (string | number)[] = [];
    for (let i = 0; i < s.length; i++) {
        const element = s[i];
        switch (element) {
            case ")":
                let count = 0;
                while (stack[stack.length - 1] !== "(") {
                    const last = stack.pop();
                    if (typeof last === "number") {
                        count = Math.max(count, last);
                    }
                }
                stack.pop();
                stack.push(count + 1);
            default:
                stack.push(element);
        }
    }
    // console.log(stack);
    const valueList = stack.filter((cur) => {
        if (typeof cur === "number") return true;
    }) as number[];
    return valueList.length ? Math.max(...valueList) : 0;
}
// @lc code=end
